package com.lhl.shiro.shiroconfig;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author LHL
 * @date 2018/1/8
 */


public class UserRealm extends AuthorizingRealm {

    private static final Logger logger = LoggerFactory.getLogger(UserRealm.class);

//    @Autowired
//    private IotUserService iotUserService;
//    @Autowired
//    private PermissionThin permissionThin;


    @Override
    public boolean supports(AuthenticationToken token) {
        //仅支持StatelessToken类型的Token
        return token instanceof UsernamePasswordToken;
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //根据用户名查找角色，请根据需求实现
//        String userIdStr = (String) principals.getPrimaryPrincipal();
//        logger.info("[UserRealm.doGetAuthorizationInfo] userId={}", userIdStr);
//        Long userId = Long.parseLong(userIdStr);
//        IotUser iotUser = iotUserService.selectByPrimaryKey(userId);
//        if (iotUser==null){
//            logger.info("[认证失败，查询不到用户信息] userId={}", userId);
//            return null;
//        }
//        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//        if (UserAuthorityConstantUtil.SUPER_ADMIN_USER_TYPE.equals(iotUser.getType())){
//            authorizationInfo.addStringPermission("*:http:*");
//        }else {
//            List<Permission> permissionList = permissionThin.selectByUserId(userId,"back");
//            for (Permission permission:permissionList){
//                if ("back".equals(permission.getPermissionType())){
//                    authorizationInfo.addStringPermission(permission.getWildcard());
//                }
//            }
//        }
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        return authorizationInfo;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        String loginUsername = token.getPrincipal().toString();
//        logger.debug("[UserRealm.doGetAuthenticationInfo] loginUsername={}", loginUsername);
//        IotUser iotUser = iotUserService.selectByUsername(loginUsername);
//        if (iotUser == null) {
//            logger.info("[账户不存在] username={}", loginUsername);
//            throw new UnknownAccountException();
//        }
//        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(loginUsername, iotUser.getPassword(), getName());
//        simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(iotUser.getSalt()));
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo("snail", "snail", getName());
        return simpleAuthenticationInfo;
    }

}
